package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.service.JazSysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazOrderDetailDao;
import com.jinzhi.jzweb.domain.JazOrderDetailDO;
import com.jinzhi.jzweb.service.JazOrderDetailService;
import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 
 * <pre>
 * 收支明细表
 * </pre>
 * <small> 2024-07-31 10:00:51 | Long</small>
 */
@Service
public class JazOrderDetailServiceImpl extends CoreServiceImpl<JazOrderDetailDao, JazOrderDetailDO> implements JazOrderDetailService {

    @Autowired
    private JazSysUserService jazSysUserService;
    @Override
    @Transactional
    public void saveOrderDetail(JazOrderDetailDO jazOrderDetailDO) {
        this.insert(jazOrderDetailDO);

    }

    @Override
    public Result<Page<JazOrderDetailDO>> selOrderDetailList(Integer type, String createTime, String endTime, int current, int size) {
        Page<JazOrderDetailDO> page = new Page<>(current, size);
        Wrapper<JazOrderDetailDO> wrapper = new EntityWrapper<>();
        if (type!=null&&!type.equals(-1)){
            wrapper.eq("type",type);
        }
        if (StringUtils.isNotBlank(createTime) && StringUtils.isNotBlank(endTime)) {
            wrapper.between("trade_time", createTime, endTime);
        }
        wrapper.orderBy("trade_time",false);
        Page<JazOrderDetailDO> jazOrderDetailDOPage = this.selectPage(page, wrapper);
        jazOrderDetailDOPage.getRecords().forEach(e->{
            UserDO userDO = jazSysUserService.selectById(e.getUserId());
            if (userDO!=null){
                e.setUserName(StringUtils.isBlank(userDO.getName())?userDO.getNickname():userDO.getName());
                e.setPhone(userDO.getMobile());
            }else {
                e.setUserName("");
                e.setPhone("");
            }
        });
        return Result.ok(jazOrderDetailDOPage);
    }

    @Override
    public Result<JazOrderDetailDO> selOrderDetailMoney(Integer type, String createTime, String endTime) {
        JazOrderDetailDO detailDO=new JazOrderDetailDO();
        Wrapper<JazOrderDetailDO> wrapper = new EntityWrapper<>();
        if (type!=null&&!type.equals(-1)){
            wrapper.eq("type",type);
        }
        if (StringUtils.isNotBlank(createTime) && StringUtils.isNotBlank(endTime)) {
            wrapper.between("trade_time", createTime, endTime);
        }
        List<JazOrderDetailDO> jazOrderDetailDOS = this.selectList(wrapper);
        //总收入
        BigDecimal totalIncome = jazOrderDetailDOS.stream().filter(e -> e.getType().equals(0)).map(JazOrderDetailDO::getMoney) // 提取money字段
                .reduce(BigDecimal.ZERO, BigDecimal::add);// 使用reduce方法累加金额

        //总支出
        BigDecimal totalExpenditure = jazOrderDetailDOS.stream().filter(e -> e.getType().equals(1)).map(JazOrderDetailDO::getMoney) // 提取money字段
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        //总净收入
        BigDecimal netIncome = totalIncome.subtract(totalExpenditure);
        detailDO.setNetIncome(netIncome);
        detailDO.setTotalIncome(totalIncome);
        detailDO.setTotalExpenditure(totalExpenditure);
        return Result.ok(detailDO);
    }

    @Override
    public List<JazOrderDetailDO> downloadImportTemplate(Integer type, String createTime, String endTime) {
        Wrapper<JazOrderDetailDO> wrapper = new EntityWrapper<>();
        if (type!=null&&!type.equals(-1)){
            wrapper.eq("type",type);
        }
        if (StringUtils.isNotBlank(createTime) && StringUtils.isNotBlank(endTime)) {
            wrapper.between("trade_time", createTime, endTime);
        }
        wrapper.orderBy("trade_time",false);
        List<JazOrderDetailDO> jazOrderDetailDOS = this.selectList(wrapper);

        jazOrderDetailDOS.forEach(e->{
            UserDO userDO = jazSysUserService.selectById(e.getUserId());
            if (userDO!=null){
                e.setUserName(StringUtils.isBlank(userDO.getName())?userDO.getNickname():userDO.getName());
                e.setPhone(userDO.getMobile());
            }else {
                e.setUserName("");
                e.setPhone("");
            }
        });
        return jazOrderDetailDOS;
    }
}
